package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.dto.CouponStaffSendNumDto;
import com.dingreading.cloud.admin.entity.CouponStaff;
import com.dingreading.cloud.admin.entity.table.CouponStaffTableDef;
import com.dingreading.cloud.admin.mapper.CouponStaffMapper;
import com.dingreading.cloud.admin.mapper.OrderFormMapper;
import com.dingreading.cloud.admin.service.CouponStaffService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.google.common.collect.HashBasedTable;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CouponStaffServiceImpl extends BaseServiceImpl<CouponStaffMapper, CouponStaff, CouponStaffTableDef> implements CouponStaffService {

    @Override
    protected CouponStaffTableDef getTable() {
        return CouponStaffTableDef.couponStaff;
    }

    @Resource
    private OrderFormMapper orderFormMapper;

    @Override
    public Page<CouponStaff> pageList(PageUtil pageUtil, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.couponName.like(keyword));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.enabled.desc(), table.id.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public void updateEnabled(Long id, Integer enabled) {
        UpdateChain.of(CouponStaff.class)
                .set(table.enabled, enabled)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public int dropData(Long id) {
        return getMapper().deleteByCondition(table.id.eq(id));
    }

    @Override
    public List<CouponStaff> getByCouponId(Long couponId, Integer enabled, String keyword, List<String> staffUids) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.couponId.eq(couponId));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.storeName.like(keyword)
                    .or(table.fullName.like(keyword)));
        if (CollectionUtils.isNotEmpty(staffUids))
            condition.and(table.staffUid.in(staffUids));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public long getCntByCouponId(Long couponId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.couponId.eq(couponId));
        return getMapper().selectCountByCondition(condition);
    }

    @Override
    public CouponStaff getStaffCoupon(String storeUid, String staffUid, Long id) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.staffUid.eq(staffUid))
                .and(table.enabled.eq(1))
                .and(table.usableMoney.gt(0))
                .and(table.id.eq(id));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public void updateEnabledByCouponId(Long couponId, Integer enabled) {
        UpdateChain.of(CouponStaff.class)
                .set(table.enabled, enabled)
                .where(table.couponId.eq(couponId))
                .update();
    }

    @Override
    public long dropDataByCouponId(Long id) {
        return getMapper().deleteByCondition(table.couponId.eq(id));
    }

    @Override
    public HashBasedTable<String, String, Integer> getStoreStaffSendNum(Long couponId, Integer enabled, List<String> staffUids) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.couponId.eq(couponId));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));
        if (CollectionUtils.isNotEmpty(staffUids))
            condition.and(table.staffUid.in(staffUids));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.storeUid, table.staffUid, QueryMethods.count(table.id).as("cnt"))
                .from(table)
                .where(condition)
                .groupBy(table.storeUid, table.staffUid);
        List<CouponStaffSendNumDto> list = getMapper().selectListByQueryAs(wrapper, CouponStaffSendNumDto.class);

        HashBasedTable<String, String, Integer> table = HashBasedTable.create();
        if (CollectionUtils.isNotEmpty(list)) {
            for (CouponStaffSendNumDto dto : list) {
                table.put(dto.getStoreUid(), dto.getStaffUid(), NepUtil.nullToZero(dto.getCnt()));
            }
        }

        return table;
    }

    @Override
    public boolean revocationMoney(String storeUid, String staffUid, Long id, double usedMoney, double usableMoney) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.staffUid.eq(staffUid))
                .and(table.id.eq(id))
                .and(table.enabled.eq(1));

        return UpdateChain.of(CouponStaff.class)
                .set(table.usedMoney, usedMoney)
                .set(table.usableMoney, usableMoney)
                .where(condition)
                .update();
    }

    @Override
    public void updateByCouponId(
            Long couponId, Long typeId, String couponName, String goodsIds, String goodsCategoryIds,
            String staffUid, String staffName
    ) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.couponId.eq(couponId))
                .and(table.usableMoney.gt(0))
                .and(table.enabled.eq(1));

        UpdateChain<CouponStaff> updateChain = UpdateChain.of(CouponStaff.class);
        if (typeId == 3) {
            updateChain.set(table.goodsIds, StringUtils.defaultString(goodsIds))
                    .set(table.goodsCategoryIds, StringUtils.defaultString(goodsCategoryIds));
        }
        if (couponName != null) {
            updateChain.set(table.couponName, couponName);
        }
        updateChain.set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName));

        updateChain.where(condition)
                .update();
    }

}
